package com.healthcare;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.Appointment;

import com.google.gson.Gson;

/**
 * Servlet implementation class UserRegistration
 */
public class ViewMyAppointments extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewMyAppointments() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String userid = (String) session.getAttribute("userid");

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		SimpleDateFormat timeformat = new SimpleDateFormat("hh:mm a");

		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aimsfirstconsultdb", "root",
					"");
			stmt = con.createStatement();

			String getUsedAppointmentSlotsSql = "select doc.first_name, doc.last_name, doc.specialization, doc.phone, "
					+ "app.appointment_date,app.appointment_id from doctors doc, appointments app where doc.doctor_id=app.doctor_id and app.user_id="
					+ userid + " order by app.appointment_date desc";

			rs = stmt.executeQuery(getUsedAppointmentSlotsSql);

			ArrayList<Appointment> myAppointments = new ArrayList<Appointment>();
			// displaying records
			while (rs.next()) {
				String firstname = rs.getObject(1).toString();
				String lastname = rs.getObject(2).toString();
				String specialization = rs.getObject(3).toString();
				String docphone = rs.getObject(4).toString();
				String appdate = rs.getObject(5).toString();
				String appointmentid = rs.getObject(6).toString();

				Appointment myAppointment = new Appointment();
				myAppointment.setAppointmentid(appointmentid);
				myAppointment.setDocname(firstname + " " + lastname);
				myAppointment.setSpecialization(specialization);
				myAppointment.setPhone(docphone);
				myAppointment.setLastname(lastname);
				myAppointment.setDate(format.parse(appdate));

				myAppointments.add(myAppointment);
			}
			request.setAttribute("myAppointments", myAppointments);

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/ViewMyAppointments.jsp");
			dispatcher.forward(request, response);
		} catch (SQLException e) {
			throw new ServletException("Servlet Could not display records.", e);
		} catch (ClassNotFoundException e) {
			throw new ServletException("JDBC Driver not found.", e);
		} catch (ParseException e) {
			throw new ServletException("Error parsing date.", e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (SQLException e) {
			}
			out.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

}
